﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;
using System.Data;

namespace Admin.WorkFlow.FormTable.TechnologyContact
{
    public partial class TechnologyContract_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProject();
                if (Request.Params["TaskBookId"] != null && Request.Params["TaskBookId"].ToString() != "")
                {
                    BindInfo(ChinaSoft.Common.Input.SetInt(Request.Params["TaskBookId"]));
                }
            }
        }
        private void BindProject()
        {
            ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo bllProject = new ChinaSoft.BLL.BLL_tb_MidSoft_ProjectInfo();
            DataSet ds=bllProject.GetAllList();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ChinaSoft.Common.BindControl.BindDropDownList(ddlProject, ds, "ProjectName", "ProjectInfoId");
            }
        }
        private void BindInfo(int taskBookId)
        {
            ChinaSoft.BLL.Common.Bind bllBind = new ChinaSoft.BLL.Common.Bind();
            DataSet ds = bllBind.GetDataSet("*", "V_TaskBookLook", "TaskBookId="+taskBookId);

            if (ds!=null&&ds.Tables.Count>0&&ds.Tables[0].Rows.Count>0)
            {
                DataRow dr=ds.Tables[0].Rows[0];
                lblCreateUserName.Text = GetProjectUserName(ChinaSoft.Common.Input.SetInt(dr["JingBanId"]));
                lblProjectContractName.Text = GetProjectUserName(ChinaSoft.Common.Input.SetInt(dr["MakeDevelopId"]));
                lblDepartContractName.Text = GetProjectUserName(ChinaSoft.Common.Input.SetInt(dr["EngineerId"]));
                lblInstituteContractName.Text = GetProjectUserName(ChinaSoft.Common.Input.SetInt(dr["SubDeanId"]));
                ddlProject.SelectedValue = dr["ProjectId"].ToString();
            }
        }
        private string GetProjectUserName(int UserId)
        {
            string strUserName = string.Empty;
            ChinaSoft.BLL.BLL_tb_MidSoft_Users bllUsers = new ChinaSoft.BLL.BLL_tb_MidSoft_Users();
            ChinaSoft.Model.Model_tb_MidSoft_Users modelUsers = bllUsers.GetModel(UserId);
            if (modelUsers != null)
            {
                strUserName = modelUsers.UserName;
            }
            return strUserName;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtConfigDate.Value.Trim().Length == 0)
            {
                strErr += "日期不能为空！\\n";
            }
            if (this.txtNumber.Text.Trim().Length == 0)
            {
                strErr += "技术联系单编号不能为空！\\n";
            }
            if (this.txtMain.Text.Trim().Length==0)
            {
                strErr += "主送单位不能为空！\\n";
            }
            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            ChinaSoft.Model.Model_tb_MidSoft_TechnologyContract modelTechContract = new ChinaSoft.Model.Model_tb_MidSoft_TechnologyContract();
            ChinaSoft.BLL.BLL_tb_MidSoft_TechnologyContract bllTechContract = new ChinaSoft.BLL.BLL_tb_MidSoft_TechnologyContract();
            modelTechContract.Attach = txtAttach.Text.Trim();
            modelTechContract.ConfirmDate = Convert.ToDateTime(txtConfigDate.Value);
            modelTechContract.ContractContent = CKEditorControl1.Text.Trim();
            modelTechContract.CreateTime = DateTime.Now;
            modelTechContract.CreateUserId = ChinaSoft.Common.Input.SetInt(Session["currentUserID"]);
            modelTechContract.MainSendUnit = txtMain.Text.Trim();
            modelTechContract.PmFlowId = ChinaSoft.Common.Input.SetInt(Request.Params["PmFlowId"]);
            modelTechContract.ProjectInfoId = ChinaSoft.Common.Input.SetInt(ddlProject.SelectedItem.Value);
            modelTechContract.SecretSendUnit = txtSecertSend.Text.Trim();
            modelTechContract.TechnologyContractNumber = txtNumber.Text.Trim();
            modelTechContract.Professional = txtProfessional.Text.Trim();
            modelTechContract.TaskBookId = ChinaSoft.Common.Input.SetInt(Request.Params["TaskBookId"]);
            bllTechContract.Add(modelTechContract);
            Response.Write("<script>alert('成功！');top.window.parent.frames.item('workspace').location.reload();window.parent.$('#DivSysiframe').dialog(\"close\");</script>");
        }
    }
}